package cn.ibizlab.odoo.core.odoo_payment.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ipayment_token;
import cn.ibizlab.odoo.core.odoo_payment.domain.Payment_token;
import cn.ibizlab.odoo.core.odoo_payment.filter.Payment_tokenSearchContext;
import cn.ibizlab.odoo.core.odoo_payment.service.IPayment_tokenService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[付款令牌] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Payment_tokenServiceImpl implements IPayment_tokenService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ipayment_tokenClientService payment_tokenClientService;

    @Override
    public boolean create(Payment_token payment_token) {
        boolean bOk = false ;
        Ipayment_token clientModel = this.convert2Model(payment_token,null);
        payment_tokenClientService.create(clientModel) ;
        this.convert2Domain(clientModel,payment_token);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //payment_tokenClientService.removeBatch(payment_tokens) ;

    }

    @Override
    public void updateBatch(List<Payment_token> payment_tokens, int batchSize) {

        //payment_tokenClientService.updateBatch(payment_tokens,batchSize) ;

    }

    @Override
    public boolean update(Payment_token payment_token) {
        boolean bOk = false ;
        Ipayment_token clientModel = this.convert2Model(payment_token,null);
        payment_tokenClientService.update(clientModel) ;
        this.convert2Domain(clientModel,payment_token) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public void createBatch(List<Payment_token> payment_tokens, int batchSize) {

                        //payment_tokenClientService.createBatch(payment_tokens,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ipayment_token clientModel = payment_tokenClientService.createModel();
        clientModel.setId(id);
        payment_tokenClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Payment_token get(Integer id) {
        Ipayment_token clientModel = payment_tokenClientService.createModel();
        clientModel.setId(id);
        payment_tokenClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Payment_token get(Payment_token payment_token) {
        Payment_token retpayment_token = this.get(payment_token.getId()) ;
    	BeanCopier copier=BeanCopier.create(retpayment_token.getClass(), payment_token.getClass(), false);
        copier.copy(retpayment_token, payment_token, null);
        return payment_token ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Payment_token> searchDefault(Payment_tokenSearchContext context) {
        List<Payment_token> list = new ArrayList<Payment_token>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_token> clientModelList = payment_tokenClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_token clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_token>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据付款收单方查询
     */
    @Override
    public Page<Payment_token> selectByOdoo_acquirer(Integer id){
        Payment_tokenSearchContext context = new Payment_tokenSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_token> list = new ArrayList<Payment_token>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_token> clientModelList = payment_tokenClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_token clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_token>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据联系人查询
     */
    @Override
    public Page<Payment_token> selectByOdoo_partner(Integer id){
        Payment_tokenSearchContext context = new Payment_tokenSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_token> list = new ArrayList<Payment_token>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_token> clientModelList = payment_tokenClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_token clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_token>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Payment_token> selectByOdoo_create(Integer id){
        Payment_tokenSearchContext context = new Payment_tokenSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_token> list = new ArrayList<Payment_token>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_token> clientModelList = payment_tokenClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_token clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_token>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Payment_token> selectByOdoo_write(Integer id){
        Payment_tokenSearchContext context = new Payment_tokenSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Payment_token> list = new ArrayList<Payment_token>();
        Page<cn.ibizlab.odoo.core.client.model.Ipayment_token> clientModelList = payment_tokenClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ipayment_token clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Payment_token>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ipayment_token convert2Model(Payment_token domain , Ipayment_token model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = payment_tokenClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getAcquirer_refDirtyFlag())
            model.setAcquirer_ref(domain.getAcquirer_ref());
         if(domain.getActiveDirtyFlag())
            model.setActive(domain.getActive());
         if(domain.getVerifiedDirtyFlag())
            model.setVerified(domain.getVerified());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getShort_nameDirtyFlag())
            model.setShort_name(domain.getShort_name());
         if(domain.getPayment_idsDirtyFlag())
            model.setPayment_ids(domain.getPayment_ids());
         if(domain.getNameDirtyFlag())
            model.setName(domain.getName());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getAcquirer_id_textDirtyFlag())
            model.setAcquirer_id_text(domain.getAcquirer_id_text());
         if(domain.getPartner_id_textDirtyFlag())
            model.setPartner_id_text(domain.getPartner_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getPartner_idDirtyFlag())
            model.setPartner_id(domain.getPartner_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getAcquirer_idDirtyFlag())
            model.setAcquirer_id(domain.getAcquirer_id());
        return model ;
    }

    public Payment_token convert2Domain( Ipayment_token model ,Payment_token domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Payment_token();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getAcquirer_refDirtyFlag())
            domain.setAcquirer_ref(model.getAcquirer_ref());
        if(model.getActiveDirtyFlag())
            domain.setActive(model.getActive());
        if(model.getVerifiedDirtyFlag())
            domain.setVerified(model.getVerified());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getShort_nameDirtyFlag())
            domain.setShort_name(model.getShort_name());
        if(model.getPayment_idsDirtyFlag())
            domain.setPayment_ids(model.getPayment_ids());
        if(model.getNameDirtyFlag())
            domain.setName(model.getName());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getAcquirer_id_textDirtyFlag())
            domain.setAcquirer_id_text(model.getAcquirer_id_text());
        if(model.getPartner_id_textDirtyFlag())
            domain.setPartner_id_text(model.getPartner_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getPartner_idDirtyFlag())
            domain.setPartner_id(model.getPartner_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getAcquirer_idDirtyFlag())
            domain.setAcquirer_id(model.getAcquirer_id());
        return domain ;
    }


}
